package J11_29;

public class test {
    public static void main(String[] args) {
        int num = 10000;
        while(num<100000){
            String str = num + "";
            if(!main1(str).equals("-1")) {
                System.out.println(str+":"+main1(str));
            }
            num++;
        }
    }

    public static String main1(String a) {
        String str = a;
        int i = 1;
        int j = 2;
        while(true){
            if(pK(str,i).equals(pK(str,j))){
                if(pK(str,i).equals(pK(str,i+1))) {
                    return pK(str,i);
                }
                else {
                    return "-1";
                }
            }
            i++;
            j+=2;
        }

    }
    public static String pK(String str,int count) {
        if(count == 0){
            return str;
        }
        int len = str.length();
        int start = Integer.parseInt(str);
        char[] nums = str.toCharArray();
        for(int i = 0;i<nums.length - 1;i++) {
            for(int j = nums.length - 1;j>i;j--){
                if(nums[j] > nums[j-1]){
                    int tmp = nums[j];
                    nums[j] = nums[j-1];
                    nums[j-1] = (char)tmp;
                }
            }
        }
        String tp = String.valueOf(nums);
        int numMax = Integer.parseInt(tp);
        for(int i = 0;i<nums.length/2;i++){
            int tmp = nums[i];
            nums[i] = nums[nums.length - i - 1];
            nums[nums.length - i - 1] = (char) tmp;
        }
        tp = String.valueOf(nums);
        int numMin = Integer.parseInt(tp);

        int ret = numMax - numMin;
        char[] ch = new char[len];
        for(int i = 0;i<len;i++){
            ch[len - i -1] = (char)('0' + ret%10);
            ret/=10;
        }

        str = String.valueOf(ch);
        return pK(str,count - 1);
    }
}
